package two.chapter_1.c1_1;

import edu.princeton.cs.algs4.StdOut;

import java.util.Arrays;

public class Ex1_1_15 {


    public static void main(String[] args) {

        int a[]=new int[5];
        a[0]=2;
        a[1]=3;
        a[2]=1;
        a[3]=1;
        a[4]=4;
        int m[]=histogram(a,5);

        int sum=0;
        for (int i=0;i<m.length;i++){
            sum+=m[i];
        }
        StdOut.println("sum:"+sum);
        StdOut.println("a.length:"+a.length);

        Printer.printArray(m);
    }

    /**
     * 更好的答案
     * @param a
     * @param M
     * @return
     */
    public static int[] histogram(int[] a, int M) {
        int[] result = new int[M];
        for (int i = 0; i < a.length; i++) {
            if (a[i] >= 0 && a[i] < M) {
                result[a[i]]++;
            }
        }
        return result;
    }
    private static int[] myhistogram(int a[],int M){
        int [] m=new int[M];
        //排序
        Arrays.sort(a);
        for (int i=0;i<m.length;i++){
            int count=0;
            for (int j=0;j<a.length;j++){
                int cmp=i-a[j];
                if (cmp==0){
                    count++;
                }

            }
            m[i]=count;

        }

        return m;
    }





}
